﻿#pragma once
#pragma once
#include<assert.h>


namespace wjc
{
	template <class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;


		vector()
			: _start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
		}
		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (const auto& e : v)
			{
				push_back(e);
			}
		}
		iterator begin()
		{
			return _start;
		}


		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}


		const_iterator end() const
		{
			return _finish;
		}
		size_t capacity()const
		{
			return _endofstorage - _start;
		}
		size_t size()const
		{
			return  _finish - _start;
		}


		void push_back(const T& a)
		{
			if (_finish == _endofstorage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}
			*_finish = a;
			_finish++;
		}


		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}


		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}


		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _endofstorage = nullptr;
			}


		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t oldsize = size();
				T* tmp = new T[n];
				if (_start)
				{
					/*memcpy(tmp, _start, sizeof(T) * oldsize);*/
					for (size_t i = 0; i < oldsize; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + oldsize;
				_endofstorage = _start + n;
			}
		}
		void resize(size_t n, T val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish < _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}




		void pop_back()
		{
			assert(size() > 0);
			--_finish;
		}
		void insert(iterator pos, T x)
		{
			assert(pos <= _finish);
			assert(pos >= _start);
			size_t len = pos - _start;
			if (_finish == _endofstorage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);//pos ʧЧ
				//posλ
				pos = _start + len;
			}
			memmove(pos + 1, pos, (_finish - pos) * sizeof(T));
			*pos = x;
			++_finish;
		}


		void erase(iterator pos)
		{
			assert(pos < _finish);
			assert(pos >= _start);
			iterator it = pos + 1;
			while (it < _finish)
			{
				*(it - 1) = *it;
				++it;
			}
			_finish--;
		}


		T& operator[](size_t pos)
		{
			assert(pos, size());
			return _start[pos];
		}
		const T& operator[](size_t pos)const
		{
			assert(pos, size());
			return _start[pos];
		}
		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
	private:


		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};


}

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace wjc
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		/*string()
			:_size(0)
			,_capacity(0)
			,_str(new char[1])
		{
			_str[1] = '\0';
		}*/
		iterator begin()
		{
			return _str;
		}


		iterator end()
		{
			return _str + _size;
		}


		const_iterator  begin()const
		{
			return _str;
		}


		const_iterator  end()const
		{
			return _str + _size;
		}
		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}


		string(const string& s)
		{
			_size = s._size;
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_capacity = s._capacity;
		}


		string& operator=(const string s)
		{
			if (this != &s)
			{
				string tmp(s);
				this->swap(tmp);
			}
			return *this;
		}


		void push_back(char c)
		{
			if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : (_capacity * 2);
				reserve(newcapacity);
			}
			_str[_size++] = c;
			_str[_size] = '\0';
		}


		string& operator+=(char c)
		{
			push_back(c);
			return *this;
		}
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* temp = new char[n + 1];
				strcpy(temp, _str);
				delete[] _str;
				_str = temp;
				_capacity = n;
			}
		}
		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			strcpy(_str + _size, str);
			_size = _size + len;
		}


		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}


		void clear()
		{
			_size = 0;
			_str[0] = '\0';
		}


		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}


		const char* c_str()const
		{
			return _str;
		}
		bool empty()const
		{
			return (_str[0] == '\0');
		}
		void resize(size_t newsize, char c = '\0')
		{
			if (newsize > _size)
			{
				if (newsize > _capacity)
				{
					reserve(newsize);
				}
				memset(_str + _size, c, newsize - _size);
			}
			_size = newsize;
			_str[newsize] = '\0';
		}

		char& operator[](size_t index)
		{
			assert(index <= _size);
			return _str[index];
		}


		const char& operator[](size_t index)const
		{
			assert(index <= _size);
			return _str[index];
		}
		//relational operators


		bool operator<(const string& s)
		{
			return strlen(_str) < strlen(s.c_str());
		}


		bool operator<=(const string& s)
		{
			return strlen(_str) <= strlen(s.c_str());
		}


		bool operator>(const string& s)
		{
			return strlen(_str) > strlen(s.c_str());
		}


		bool operator>=(const string& s)
		{
			return strlen(_str) >= strlen(s.c_str());
		}


		bool operator==(const string& s)
		{
			return !strcmp(_str, s.c_str());
		}


		bool operator!=(const string& s)
		{
			return strcmp(_str, s.c_str());
		}
		// 返回c在string中第一次出现的位置


		size_t find(char c, size_t pos = 0) const
		{
			for (int i = pos; i < _size; i--)
			{
				if (_str[pos] == c)
					return i;
			}
			return npos;
		}


		// 返回子串s在string中第一次出现的位置


		size_t find(const char* s, size_t pos = 0) const
		{
			const char* p = strstr(_str + pos, s);
			if (p == nullptr)
			{
				return npos;
			}
			else
			{
				return p - _str;
			}
		}


		// 在pos位置上插入字符c/字符串str，并返回该字符的位置


		string& insert(size_t pos, char c)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}
			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}
			_str[pos] = c;
			_size++;
			return *this;
		}


		string& insert(size_t pos, const char* str)
		{
			assert(pos < _size);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			size_t end = _size;
			while (end >= pos)
			{
				_str[end + len] = _str[end];
				--end;
			}
			strncpy(_str + _size, str, len);
			_size += len;
		}






		// 删除pos位置上的元素，并返回该元素的下一个位置


		string& erase(size_t pos, size_t len);
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_capacity = _size = 0;
		}


		const char* c_str()
		{
			return _str;
		}
		size_t size()
		{
			return _size;
		}
		size_t capacity()
		{
			return _capacity;
		}


	private:
		size_t _size;
		size_t _capacity;
		char* _str;


		const static size_t npos = -1;
	};


	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto e : s)
		{
			out << e;
		}
		return out;
	}


	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char buf[128];
		char ch = in.get();
		int i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buf[i++] = ch;
			if (i == 127)
			{
				buf[i] = '\0';
				s += buf;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buf[i] = '\0';
			s += buf;
		}
		return in;
	}





}
